#include <my_header.h>

/* Usage: ./19_rwlock_wrlock */
pthread_rwlock_t rLock = PTHREAD_RWLOCK_INITIALIZER;
void *func(void *arg)
{
    // 写锁是排他的
    pthread_rwlock_wrlock(&rLock);
    printf("child thread is running...\n");
    sleep(3);
    printf("over \n");
    pthread_rwlock_unlock(&rLock);
    return NULL;
}

int main(void){


    // 创建子线程
    pthread_t tid;
    int ret_create = pthread_create(&tid, NULL, func, NULL);
    THREAD_ERROR_CHECK(ret_create, "pthread_create");

    // 加写锁再次写会阻塞
    pthread_rwlock_wrlock(&rLock);
    // 加读锁再次写会阻塞
    printf("main is runnning...\n");
    sleep(3);
    pthread_rwlock_unlock(&rLock);


    // 捕获、阻塞 并清理资源
    int ret_join = pthread_join(tid, NULL);
    THREAD_ERROR_CHECK(ret_join, "pthread_join");

    pthread_rwlock_destroy(&rLock);
    return 0;
}


